// 入口文件
console.log('OK')
import Vue from 'vue'
import app from './App.vue'

import VueRouter from 'vue-router'
Vue.use(VueRouter)

import router from './router.js'

import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'

Vue.use(MintUI)

import './lib/mui/css/mui.css'
import './lib/mui/css/icons-extra.css'

import VueResource from 'vue-resource'
Vue.use(VueResource)

import Vuex from 'vuex'
Vue.use(Vuex)

let car = JSON.parse(localStorage.getItem('car') || '[]')

const store = new Vuex.Store({
    state: {//用来存取数据，this.$store.state.***访问
      car: car
    },
    mutations: {
      addToCar(state,goodsInfo){
        // let flag = false;
        let flag = state.car.some(function(item){
          if(item.id==goodsInfo.id){
            item.count+=parseInt(goodsInfo.count)
            // flag = true;
            return true;
          }
        });

        if(!flag){
          state.car.push(goodsInfo)
        }
        //保存到本地
        localStorage.setItem('car',JSON.stringify(state.car))
      }
    },
    getters: {
      getAllcount(state){
        let c = 0;
        state.car.forEach(function(item){
          c+=item.count;
        })
        return c
      }
    }
  })

//设置路由根路径
Vue.http.options.root = 'http://192.168.6.224:8080';
// This will send the request as application/x-www-form-urlencoded MIME type, as if from an normal HTML form.
Vue.http.options.emulateJSON = true;

//导入格式化时间的插件
import moment from 'moment'
Vue.filter('dateFormat',function(dataStr,pattern = "YYYY-MM-DD HH:mm:ss"){
    return moment(dataStr).format(pattern)
})

var vm = new Vue({
    el:'#app',
    render:c=>c(app),
    router,
    store
})
